/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings;

import com.android.settings.R;

import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;

import android.util.Log;

import com.hisense.settings.util.Wrapper;

/*import com.marvell.willowtv.framework.features.ParentalControl;
import com.marvell.willowtv.framework.features.TvFramework;
import com.marvell.willowtv.framework.model.atsc.USDTVUSRating;
import com.marvell.willowtv.framework.util.atsc.PCEnumClass.*;*/

import android.content.SharedPreferences;

/**
 * Handles settings menu for channel settings.
 *
 * @author huangjiufa
 */
public class USTVRatingSettings extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener {

    private static final String TAG = "USTVRatingSettings";
    private static String test = "";

    //string for age-based
    private static final String KEY_US_TV_RATING_AGE = "us_tv_rating_age";
    //string for Fantasy violence
    private static final String KEY_US_TV_RATING_FV = "us_tv_rating_fv";
    //string for Violence
    private static final String KEY_US_TV_RATING_V = "us_tv_rating_v";
    //string for Sexual situations
    private static final String KEY_US_TV_RATING_S = "us_tv_rating_s";
    //string for Adult language
    private static final String KEY_US_TV_RATING_L = "us_tv_rating_l";
    //string for Sexual suggestive dialog
    private static final String KEY_US_TV_RATING_D = "us_tv_rating_d";

    // for age-based
    private ListPreference mUSTVRatingAgePreference;
    // for Fantasy violence
    private ListPreference mUSTVRatingFvPreference;
    // for Violence
    private ListPreference mUSTVRatingVPreference;
    // for Sexual situations
    private ListPreference mUSTVRatingSPreference;
    // for Adult language
    private ListPreference mUSTVRatingLPreference;
    // for Sexual suggestive dialog
    private ListPreference mUSTVRatingDPreference;

    private static final String[] st_summary = {"None", "TV-Y", "TV-Y7", "TV-G", "TV-PG", "TV-14", "TV-MA"};


    private static final int VALUE_DEFAULT_US_TV_RATING_AGE = 0;
    // 0 None
    // 1 G
    // 2 PG
    // 3 PG-13
    // 4 R
    // 5 NC-17
    // 6 X
    private static final int VALUE_DEFAULT_US_TV_RATING_FV = 0;
    // 0 E
    // 1 C
    // 2 C8+
    // 3 G
    // 4 PG
    // 5 14+
    // 6 18+
    private static final int VALUE_DEFAULT_US_TV_RATING_V = 0;
    // 0 E
    // 1 G
    // 2 8ans+
    // 3 13ans+
    // 4 16ans+
    // 5 18ans+
    private static final int VALUE_DEFAULT_US_TV_RATING_S = 0;
    private static final int VALUE_DEFAULT_US_TV_RATING_L = 0;
    private static final int VALUE_DEFAULT_US_TV_RATING_D = 0;


    /*private ParentalControl mParentalControlSettings;
    private USDTVUSRating mUSDTVusRating = null;
    private RegionId rd = RegionId.REG_USA;

    private int mTvRating = PCUSTVRatings.TV_NULL.ordinal();
    private int mChildRating = PCUSChildRatings.TV_Y_NULL.ordinal();
    private int mViolenceRating = PCUSViolenceRatings.VIO_NULL.ordinal();
    private int mFantasyViolenceRating = PCUSChildFVRatings.TV_Y7_NULL.ordinal();
    private int mSexRating = PCUSSexRatings.SEX_NULL.ordinal();
    private int mLanguageRating = PCUSLanguageRatings.LAN_NULL.ordinal();
    private int mDialogueRating = PCUSDialogRatings.DIA_NULL.ordinal();*/
    private SharedPreferences.Editor editor;
    private int mTvOrChildFlag = 0;

    private int allowrc = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.us_tv_rating_settings);
        //mParentalControlSettings = TvFramework.getInstance()
        //        .getParentalControlInstance();

    }


    @Override
    protected void onResume() {
        super.onResume();

        updateUSTVRating();

        mUSTVRatingAgePreference = (ListPreference) findPreference(KEY_US_TV_RATING_AGE);
        final int currentUSTVRatingAge = getUSTVRatingAge(VALUE_DEFAULT_US_TV_RATING_AGE);
        Log.d(TAG, "onResume--------ggggg---------currentUSTVRatingAge=" + currentUSTVRatingAge);
        mUSTVRatingAgePreference.setValue((String.valueOf(currentUSTVRatingAge)));
        mUSTVRatingAgePreference.setOnPreferenceChangeListener(this);
        updateUSTVRatingAgeSummary((String.valueOf(currentUSTVRatingAge)));

        mUSTVRatingFvPreference = (ListPreference) findPreference(KEY_US_TV_RATING_FV);
        final int currentUSTVRatingFv = getUSTVRatingFv(VALUE_DEFAULT_US_TV_RATING_FV);
        Log.d(TAG, "onResume-----------------currentUSTVRatingFv=" + currentUSTVRatingFv);
        mUSTVRatingFvPreference.setValue((String.valueOf(currentUSTVRatingFv)));
        mUSTVRatingFvPreference.setOnPreferenceChangeListener(this);

        updateUSTVRatingFvSummary((String.valueOf(currentUSTVRatingFv)));

        mUSTVRatingVPreference = (ListPreference) findPreference(KEY_US_TV_RATING_V);
        int currentUSTVRatingV = getUSTVRatingV(VALUE_DEFAULT_US_TV_RATING_V);
        Log.d(TAG, "onResume-----------------currentUSTVRatingV=" + currentUSTVRatingV);

        mUSTVRatingVPreference.setValue((String.valueOf(currentUSTVRatingV)));
        mUSTVRatingVPreference.setOnPreferenceChangeListener(this);


        updateUSTVRatingVSummary((String.valueOf(currentUSTVRatingV)));

        mUSTVRatingSPreference = (ListPreference) findPreference(KEY_US_TV_RATING_S);
        int currentUSTVRatingS = getUSTVRatingS(VALUE_DEFAULT_US_TV_RATING_S);

        mUSTVRatingSPreference.setValue((String.valueOf(currentUSTVRatingS)));
        mUSTVRatingSPreference.setOnPreferenceChangeListener(this);

        updateUSTVRatingSSummary((String.valueOf(currentUSTVRatingS)));

        mUSTVRatingLPreference = (ListPreference) findPreference(KEY_US_TV_RATING_L);
        int currentUSTVRatingL = getUSTVRatingL(VALUE_DEFAULT_US_TV_RATING_L);


        mUSTVRatingLPreference.setValue((String.valueOf(currentUSTVRatingL)));
        mUSTVRatingLPreference.setOnPreferenceChangeListener(this);

        updateUSTVRatingLSummary((String.valueOf(currentUSTVRatingL)));

        mUSTVRatingDPreference = (ListPreference) findPreference(KEY_US_TV_RATING_D);
        final int currentUSTVRatingD = getUSTVRatingD(VALUE_DEFAULT_US_TV_RATING_D);

        mUSTVRatingDPreference.setValue((String.valueOf(currentUSTVRatingD)));
        mUSTVRatingDPreference.setOnPreferenceChangeListener(this);
        updateUSTVRatingDSummary((String.valueOf(currentUSTVRatingD)));


    }


    private void updateUSTVRating() {

        /*mUSDTVusRating = mParentalControlSettings.getUSParentalRating();
        if (mUSDTVusRating != null) {
            mTvRating = mUSDTVusRating.getTvRating();
            mChildRating = mUSDTVusRating.getChildRating();
            mViolenceRating = mUSDTVusRating.getDimViolence();
            mFantasyViolenceRating = mUSDTVusRating.getChildFVRating();
            mSexRating = mUSDTVusRating.getDimSex();
            mLanguageRating = mUSDTVusRating.getDimLang();
            mDialogueRating = mUSDTVusRating.getDimDiag();
            Log.d(TAG, "---updateUSTVRatin--mTvRating=" + mTvRating
                    + "----mChildRating=" + mChildRating
                    + "---mViolenceRating=" + mViolenceRating
                    + "---mFaViolenceRating=" + mFantasyViolenceRating
                    + "----mSexRating=" + mSexRating
                    + "----mLanguageRating=" + mLanguageRating
                    + "-----mDialogueRating=" + mDialogueRating);
        }*/
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

        return super.onPreferenceTreeClick(preferenceScreen, preference);

    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        Wrapper.getWrapperInstance().setPCRatingEnable(1);
        Wrapper.getWrapperInstance().setPCEnable(1);
        final String key = preference.getKey();

        if (KEY_US_TV_RATING_AGE.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
                //syn_other_rating_summary(preference,objValue);
                setUSTVRatingAge(value);
                updateUSTVRatingAgeSummary(objValue);
                syn_other_rating_summary_new(preference, value);

            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        } else if (KEY_US_TV_RATING_FV.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            Log.d(TAG, "onpreference change----RATING_FV-----55555555555555value=" + value);
            try {
                setUSTVRatingFv(value);
                updateUSTVRatingFvSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        } else if (KEY_US_TV_RATING_V.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            Log.d(TAG, "RATING_V----------66666666666666value=" + value);
            try {
                setUSTVRatingV(value);
                updateUSTVRatingVSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        } else if (KEY_US_TV_RATING_L.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            Log.d(TAG, "_RATING_L--------777777777777value=" + value);
            try {
                setUSTVRatingL(value);
                updateUSTVRatingLSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        } else if (KEY_US_TV_RATING_S.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            Log.d(TAG, "-RATING_S.---------888888888888value=" + value);
            try {
                setUSTVRatingS(value);
                updateUSTVRatingSSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        } else if (KEY_US_TV_RATING_D.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            Log.d(TAG, "RATING_D----------999999999999999value=" + value);
            try {
                setUSTVRatingD(value);
                updateUSTVRatingDSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        return true;
    }


    private boolean handleSelectItem(Object newValue) {

        return false;
    }


    private int getUSTVRatingAge(int defaultValue) {
        int value = defaultValue;
        SharedPreferences USTVRatingSettings = getSharedPreferences("com.google.tv.settings_preferences", Context.MODE_PRIVATE);
        int mTCflag = USTVRatingSettings.getInt("pc_tv_child_flag", 0);
        
       /* Log.d(TAG, "--------getUSTVRatingAge--mTvRating=" + mTvRating + "--------" + "mTv OR Child flag=" + mTCflag);
        if (mTCflag == 1) {//1 means child, 0 means value > Tv_G
            if (mChildRating >= PCUSChildRatings.TV_Y_NULL.ordinal() && mChildRating <= PCUSChildRatings.TV_Y7.ordinal()) {
                value = mChildRating;
                Log.d(TAG, "--------getUSTVRatingAge--mChildRating=" + mChildRating);
            }
        } else {
            if (mTvRating > PCUSTVRatings.TV_NULL.ordinal() && mTvRating <= PCUSTVRatings.TV_MA.ordinal()) {
                value = mTvRating + 2;//[Tv-G ,TV-MA] = [2,6],  mTvRating->[0,4]
                                      // ==> mTvRating+2 ->[2,6]
                Log.d(TAG, "--------getUSTVRatingAge--mTvRating=" + mTvRating);
            }

        }
        Log.d(TAG, "--------getUSTVRatingAge--mChildRating=" + mChildRating);*/

        return value;
    }

    /*
     * age
     * <li>when value == 0 means unlock all</li>
     * <li>when value > 1 means unlock below "value" , lock "value"</li>
     * <li>when value = 1 means lock all</li>
     */
    private void setUSTVRatingAge(int defaultValue) {
        int value = defaultValue;//defaultValue ->[0,6], 2 means Tv-Y7, childTv ->[0,2]
        /*
        if (defaultValue <= 2) {
            if (defaultValue != 0) {
                mTvOrChildFlag = 1;
                SharedPreferences USTVRatingSettings = getSharedPreferences("com.google.tv.settings_preferences", Context.MODE_PRIVATE);
                editor = USTVRatingSettings.edit();
                editor.putInt("pc_tv_child_flag", mTvOrChildFlag);
                editor.commit();
                if (defaultValue == 2) {
                    setPCRating(PCDimensionRating.US_CHILD, defaultValue - 1, false);
                    setPCRating(PCDimensionRating.US_CHILD, defaultValue, true);
                } else {
                    setPCRating(PCDimensionRating.US_CHILD, defaultValue, true);
                }

                Log.d(TAG, "------111111111111---lock input pram ture");
            }
            if (defaultValue == 0) {
                // setPCRating(PCDimensionRating.US_CHILD, 3, false);
                setPCRating(PCDimensionRating.US_TV, 4, false);
                Log.d(TAG, "------222222222---unlock input pram false");
            }

        } else {
            setPCRating(PCDimensionRating.US_CHILD, 3, false);
            if ((defaultValue - 3) > 0) {
                setPCRating(PCDimensionRating.US_TV, defaultValue - 3, false);
            }
            setPCRating(PCDimensionRating.US_TV, defaultValue - 2, true);
            mTvOrChildFlag = 0;
            SharedPreferences USTVRatingSettings = getSharedPreferences("com.google.tv.settings_preferences", Context.MODE_PRIVATE);
            editor = USTVRatingSettings.edit();
            editor.putInt("pc_tv_child_flag", mTvOrChildFlag);
            editor.commit();
            Log.d(TAG, "333333333333333333333333333333");
        }*/

    }
    /*
     * Age
     */
    private void updateUSTVRatingAgeSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.us_tv_rating_age_entries);
        CharSequence[] values = mUSTVRatingAgePreference.getEntryValues();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                mUSTVRatingAgePreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    /*
     * <t>fantasy violence</t>
     * <p>1 means lock, 0 means unlocked </p>
     */
    private int getUSTVRatingFv(int defaultValue) {
        int value = defaultValue;
        //value = mFantasyViolenceRating;
        if (value == 1) {
            value = 0;
        } else {
            value = 1;
        }

        return value;
    }

    /*
      fantasy violence
     */
    private void setUSTVRatingFv(int defaultValue) {
        int value = defaultValue;

        /*if (defaultValue == 0) {
            setPCRating(PCDimensionRating.US_CHILD_FV, PCUSChildFVRatings.TV_Y7_FV.ordinal(), false);
            return;
        }
        setPCRating(PCDimensionRating.US_CHILD_FV, PCUSChildFVRatings.TV_Y7_FV.ordinal(), true);*/

    }

    /*
      fantasy violence
     */
    private void updateUSTVRatingFvSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.us_tv_rating_fv_entries);
        CharSequence[] values = mUSTVRatingFvPreference.getEntryValues();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                mUSTVRatingFvPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    /*
      violence
     */
    private int getUSTVRatingV(int defaultValue) {
        int value = defaultValue;
        /*value = mViolenceRating;
        if (value == 4) {
            value = 0;
        }*/
        return value;
    }

    /*
     * violence
     * <li>when value == 0 means unlock all</li>
     * <li>when value > 1 means unlock less than "value" , lock "value"</li>
     * <li>when value = 1 means lock all</li>
     */
    private void setUSTVRatingV(int defaultValue) {
        /*int value = defaultValue;
        if (value == 0) {
            setPCRating(PCDimensionRating.US_VIO, 3, false);
            return;
        }
        Log.d(TAG, "setUSTVRatingV-------------vvvvvvvvvvvvv=------");
        if (value > 1) {
            setPCRating(PCDimensionRating.US_VIO, defaultValue - 1, false);
        }

        setPCRating(PCDimensionRating.US_VIO, defaultValue, true);*/
    }

    /*
      violence
     */
    private void updateUSTVRatingVSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.us_tv_rating_v_entries);
        CharSequence[] values = mUSTVRatingVPreference.getEntryValues();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                mUSTVRatingVPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    /*
        language
     */
    private int getUSTVRatingL(int defaultValue) {
        int value = defaultValue;
        /*value = mLanguageRating;
        if (value == 4) {
            value = 0;
        }*/
        return value;
    }

    /*
     *  language
     * <li>when value == 0 means unlock all</li>
     * <li>when value > 1 means unlock < value , lock value</li>
     * <li>when value = 1 means lock all</li>
     */
    private void setUSTVRatingL(int defaultValue) {
        /*int value = defaultValue;
        if (value == 0) {
            setPCRating(PCDimensionRating.US_LAN, 3, false);
            return;
        }
        if (value > 1) {
            setPCRating(PCDimensionRating.US_LAN, defaultValue - 1, false);
        }
        // TODO huangjiufa
        setPCRating(PCDimensionRating.US_LAN, defaultValue, true);*/

    }

    /*
        language
     */
    private void updateUSTVRatingLSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.us_tv_rating_l_entries);
        CharSequence[] values = mUSTVRatingLPreference.getEntryValues();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                mUSTVRatingLPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    /*
      sexual
     */
    private int getUSTVRatingS(int defaultValue) {
        int value = defaultValue;
        /*value = mSexRating;
        if (value == 4) {
            value = 0;
        }*/
        return value;
    }

    /*
     * sexual
     * <li>when value == 0 means unlock all</li>
     * <li>when value > 1 means unlock < value , lock value</li>
     * <li>when value = 1 means lock all</li>
     */
    private void setUSTVRatingS(int defaultValue) {
        /*int value = defaultValue;
        if (value == 0) {
            setPCRating(PCDimensionRating.US_SEX, 3, false);
            return;
        }
        // TODO huangjiufa
        if (value > 1) {
            setPCRating(PCDimensionRating.US_SEX, defaultValue - 1, false);
        }
        setPCRating(PCDimensionRating.US_SEX, defaultValue, true);*/

    }

    /*
      sexual
     */
    private void updateUSTVRatingSSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.us_tv_rating_s_entries);
        CharSequence[] values = mUSTVRatingSPreference.getEntryValues();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                mUSTVRatingSPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    /*
      sexual suggestive dialog
     */
    private int getUSTVRatingD(int defaultValue) {
        int value = defaultValue;
        /*value = mDialogueRating;
        if (value == 4 || value == 3) {
            value = 0;
        }*/
        return value;
    }

    /*
     * <p>sexual suggestive dialog</p>
     * <li>when value == 0 means unlock all</li>
     * <li>when value > 1 means unlock < value , lock value</li>
     * <li>when value = 1 means lock all</li>
     */
    private void setUSTVRatingD(int defaultValue) {
        int value = defaultValue;
/*
        if (value == 0) {
            setPCRating(PCDimensionRating.US_DIA, 2, false);
            return;
        }
        if (value > 1) {
            setPCRating(PCDimensionRating.US_DIA, defaultValue - 1, false);
        }
        setPCRating(PCDimensionRating.US_DIA, defaultValue, true);*/
    }

    /*
      sexual suggestive dialog
     */
    private void updateUSTVRatingDSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.us_tv_rating_d_entries);
        CharSequence[] values = mUSTVRatingDPreference.getEntryValues();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                mUSTVRatingDPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    /*private void setPCRating(PCDimensionRating pcDimension, int pcRating, boolean lock) {
        Log.d(TAG, "setPCRating dimension=" + pcDimension + " rating=" + pcRating + " lock=" + lock);
        mParentalControlSettings.setUSParentalRating(pcDimension, pcRating, lock);
        //Combine Child and Adult TV rating, block adult if block child, unblock child if unblock adult

        if (pcDimension == PCDimensionRating.US_CHILD && lock ) {
            mParentalControlSettings.setUSParentalRating(PCDimensionRating.US_TV, PCUSTVRatings.TV_G.ordinal(), true);
        } else if (pcDimension == PCDimensionRating.US_TV && !lock) {
            mParentalControlSettings.setUSParentalRating(PCDimensionRating.US_CHILD,
                    PCUSChildRatings.TV_Y7.ordinal(), false);
        }
    }*/

    private void syn_other_rating_summary_new(Preference preference, int objValue) {
        /*updateUSTVRating();
        int fv_value = getUSTVRatingFv(objValue);
        int v_value = mViolenceRating;
        int sex_value = mSexRating;
        int lan_value = mLanguageRating;
        int dia_value = mDialogueRating;

        Log.d(TAG, "sync----fv_value=" + fv_value
                + "---v_value=" + v_value
                + "----sex_value=" + sex_value
                + "----lan_value=" + lan_value
                + "-----dia_value=" + dia_value);
        if (v_value == 4) {
            v_value = 0;
        }
        if (sex_value == 4) {
            sex_value = 0;
        }
        if (lan_value == 4) {
            lan_value = 0;
        }
        if (dia_value == 4 || dia_value == 3) {
            dia_value = 0;
        }

        updateUSTVRatingFvSummary((String.valueOf(fv_value)));
        mUSTVRatingFvPreference.setValue((String.valueOf(fv_value)));

        updateUSTVRatingVSummary((String.valueOf(v_value)));
        mUSTVRatingVPreference.setValue((String.valueOf(v_value)));

        updateUSTVRatingLSummary((String.valueOf(lan_value)));
        mUSTVRatingLPreference.setValue((String.valueOf(lan_value)));

        updateUSTVRatingSSummary((String.valueOf(sex_value)));
        mUSTVRatingSPreference.setValue((String.valueOf(sex_value)));

        updateUSTVRatingDSummary((String.valueOf(dia_value)));
        mUSTVRatingDPreference.setValue((String.valueOf(dia_value)));*/

    }


}
